
#include<string>
#include<map>
#include"udp_server.hpp"


struct user
{
    std::string _ip;
    uint16_t _port;
    user(std::string ip, uint16_t port)
        :_ip(ip), _port(port)
    {
    }
};



class users{
public:

    void insert(std::string ip, uint16_t port)
    {
        umap.insert(make_pair(ip+std::to_string(port), user(ip,port)));
    }

    void del(std::string ip , uint16_t port)
    {
        umap.erase(ip+std::to_string(port));
    }

    bool isonline(std::string ip , uint16_t port)
    {
        return umap.find(ip+std::to_string(port)) != umap.end();
    }

    void groupinfo(int& _serverfd, std::string& message)
    {
        //广播
        for(auto & e : umap)
        {
            struct sockaddr_in c_addr;
            bzero(&c_addr, sizeof(c_addr));
            c_addr.sin_family = AF_INET;
            c_addr.sin_port = htons(e.second._port);
            c_addr.sin_addr.s_addr = inet_addr(e.second._ip.c_str());
            sendto(_serverfd, message.c_str(), message.size(), 0 , (struct sockaddr*)&c_addr, sizeof(c_addr));
        }
    }
private:
    std::map<std::string, user> umap;
};
